$(function () {
        $("tr:even").css("background-color"," #DCDCDC ");
        type=$("select[name='type']").val();
        project_code=$("input[name='project_code']").val();
        $(".sub").click(function(event) {//添加至session['module']数组
          var name=$("select[name='module_name'] option:selected").text();
          if(name=="请选择"){
            $("#promot p").text("请选择模块");
          }else{
            var baseprice=$("input[name='baseprice']").val();
            var safeprice=$("input[name='safeprice']").val();
            var classe=$("select[name='class']").val();
            var buynumber=$("input[name='buynumber']").val();
            var number=$("select[name='number']").val();
            var basenum=$("input[name='basenum']").val();
            var remark=$("td[name='remark']").attr("title");
            customAjax(url,{status:'add',name:name,type:type,baseprice:baseprice,safeprice:
              safeprice,discount:'0%',buynumber:buynumber,basenum:basenum,class:classe,number:number,
              remark:remark,project_code:project_code},function(data){
              $("#promot p").text(data.msg);
            });
          }
          $("#promot").animate({opacity:1}, 1000);
          $("#promot").animate({opacity:0}, 1000);     
        });
        
        $(".add").click(function(event) {//添加至数据库
          var total=0;
          customAjax(url,{status:'save',type:type,project_code:project_code},function(data){
            if(data.code=="ok"){
              for(var x in data.data){
                if(data.data[x]["buynumber"]>50){
               	  overflow=(data.data[x]["buynumber"]-data.data[x]["basenum"])*data.data[x]["safeprice"];
               	  total=total+parseFloat(data.data[x]["singleprice"])+parseFloat(overflow);
                }else{
                  total=parseFloat(data.data[x]["singleprice"]);
                }
              }
              total=total.toFixed(2);
              $("input[name='ecost']").val(total);
            }else{
              $("#promot p").text(data.msg);
              $("#promot").animate({opacity:1}, 1000);
              $("#promot").animate({opacity:0}, 1000); 
            }
          });
        });

        $("input[name='ocost']").blur(function(event) {
          var Ocost=$(this).val();
          var Ecost=$("input[name='ecost']").val();
          var diff=(Ecost-Ocost);
          if(Ocost&&Ecost){
            $("input[name='diff']").val(diff.toFixed(2));
          }
        });

        $(".look").click(function(event) {
          $(".tlook").empty();
          customAjax(url,{status:'look'},function(data){
            for(var x in data.data){
              $(".tlook").append(
                '<tr><td>'+data.data[x]["module_name"]+'</td><td>'+data.data[x]["number"]+'</td><td>'+data.data[x]["class"]+'</td>'+
                    '<td>'+data.data[x]["singleprice"]+'</td><td>'+data.data[x]["safeprice"]+'</td><td>'+data.data[x]["buynumber"]+'</td><td>'+data.data[x]["basenum"]+'</td><td>'+
                data.data[x]["type"]+'</td></tr>'
              );
            }
          });
        });

        $(".empty").click(function(event) {
          $(".tlook").empty();
          customAjax(url,{status:'empty'},function(data){
            $("#promot p").text(data.msg);
          });
            $("#promot").animate({opacity:1}, 1000);
            $("#promot").animate({opacity:0}, 1000); 
        });

        $("select[name='number']").change(function(event) {
        	$("select[name='module_name']").empty();
          $("input[name='baseprice']").val("");
          $("input[name='safeprice']").val("");
          $("input[name='basenum']").val("");
          $("input[name='buynumber']").val("");
          $("select[name='class']").val("请选择");
        	number=$(this).val();
        	$("select[name='module_name']").append('<option value=请选择>请选择</option>');
        	customAjax(url,{status:"name",number:number},function(data){
                for(var x in data.data){
                	$("td[name='remark']").attr("title",data.data[x]["remark"]);
                	$("select[name='module_name']").append('<option value='+
                    data.data[x]["number"]+'>'+data.data[x]["module_name"]+'</option>');
                }
        	})
        });

        $("select[name='module_name']").change(function(event) {
          $("select[name='class']").val("请选择");
          $("input[name='basenum']").val("");
          $("input[name='baseprice']").val("");
          $("input[name='safeprice']").val("");
        });
       
        $("select[name='class']").change(function(event){
          $("input[name='baseprice']").val("");
          $("input[name='safeprice']").val("");
          $("input[name='buynumber']").val("");
          $("input[name='basenum']").val("");
          var number=$("select[name='module_name']").val();
          var name=$("select[name='module_name']").find("option:selected").text();
          customAjax(url,{class:$(this).val(),status:'price',type:type,number:number,name:name},
            function(data){
              if(data.data){
                $("input[name='baseprice']").val(data.data['baseprice']);
                $("input[name='basenum']").val(data.data['basenum']);
              }else{
                $("input[name='baseprice']").val("暂无价格");
              }if(data.data){
                $("input[name='safeprice']").val(data.data['safeprice']);
              }else{
                $("input[name='safeprice']").val("暂无价格");
              }
            });    
        });
        
        $(".lableft").click(function(event) {
          $(".labright").css("background-color","#CCCCCC");
          $(".lableft").css("background-color","#3399CC");
          $(".lableft").find("span").css("color","white");
          $(".labright").find("span").css("color","gray");
        });

        $(".labright").click(function(event) {
          $(".lableft").css("background-color","#CCCCCC");
          $(".labright").css("background-color","#3399CC");
          $(".labright").find("span").css("color","white");
          $(".lableft").find("span").css("color","gray");
        });
      

        function customAjax(url,data,callback){
              $.ajax({
              	url: url,
              	type: 'post',
              	dataType: 'json',
              	data: data,
              })
              .success(function(msg) {
              	if(msg){
              	  callback(msg);
              	}else{
              	  callback({error : "暂无数据"});
              	}
              })
              .fail(function() {
              	console.log("error");
              })
              .always(function() {
              	console.log("complete");
              });
        }

})
